package com.javabatch.petstore.fixtures;

import java.text.DecimalFormat;
import java.util.List;

import com.javabatch.petstore.process.CreateInvoiceProcess;

/**
 * Simple file generator for use with fitnesse.
 *
 * @author eco
 */
public class CreateInvoiceProcessFixture {
    /** Customer file. */
   
	private  double executionTimeInMilliseconds;
	private  DecimalFormat rounded = new DecimalFormat("#0.00");
	private long ordersProcessed;
	private long invoicedProcessed;
    private CreateInvoiceProcess process;
	
    public CreateInvoiceProcessFixture(final String orderFile, String customerFile, String catalogFile, String invoiceFile) {
        process = new CreateInvoiceProcess(orderFile, customerFile, catalogFile, invoiceFile);
    }

    public  boolean createInvoices() {
    	long startTime = System.currentTimeMillis();
    	process.prepare(null);
    	List<Long> result = process.execute(null);
    	process.release(null);

    	
    	ordersProcessed = result.get(0);	
    	invoicedProcessed = result.get(1);
    	
    	executionTimeInMilliseconds = System.currentTimeMillis() - startTime;
   	return true;
    }
    
    public String executionTime() {

		double secs = executionTimeInMilliseconds/1000;
		return rounded.format(secs) + " seconds";
	}
	
	public String throughput() {
		return ordersProcessed + " orders processed";
	}
	
	public String ordersPrSecond() {
		double recprSec = ordersProcessed*1000 / executionTimeInMilliseconds;
		return rounded.format(recprSec);
	}
	
	public String invoicesCreated() {
		return Long.toString(invoicedProcessed);
	}
}
